home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-05-21 | 11.6 KB | 435 lines |
- /* $Id: MapPanel.java,v 1.7 1996/03/31 11:43:02 djun Exp djun $
-
- File: MapPanel.java
-
- Author: Djun M. Kim
- Copyright (c) 1996 Djun M. Kim. All rights reserved.
-
- */
-
- import java.applet.Applet;
- import java.awt.image.*;
- import java.awt.*;
- import java.util.*;
- import Location;
- import MapError;
-
- class MapPanel extends gridPanel implements Runnable {
-
- MapInfo parent;
-
- // The tread running this panel
- Thread runner;
-
- MapCanvas canvas;
- ScaleCtrls scale_ctrls;
-
- MapPanel(MapInfo target) {
- super(target);
- this.parent = target;
- canvas = new MapCanvas(parent);
- scale_ctrls = new ScaleCtrls(parent);
-
- // Panel gridx gridy Weightx Weighty GridW GridH
- make_canvas(canvas, 1, 1, 1.0, 1.0, REMN, 1);
- make_panel(scale_ctrls, 1, 2, 1.0, 0.0, REMN, 1);
- };
-
-
- // Methods to handle the thread
- public void start () {
- if (runner == null) {
- runner = new Thread(this);
- runner.start();
- }
- }
-
- public void stop () {
- if (runner != null) {
- runner.stop();
- runner = null;
- }
- }
-
- public void run() {
- canvas.repaint();
- }
- }
-
-
- class MapCanvas extends Canvas {
-
- MapInfo parent;
-
- MapError error_handler = new MapError();
-
- // Instance variables for double buffering
- private Image hiddenimagebuffer;
- private Graphics hiddengraphics;
-
- private Font trb18 = new Font("TimesRoman", Font.BOLD, 18);
-
- private Location loc; // location of pointer on the map
- private flashingLocation selected_location; // current selected location
-
- final int xsize = parent.map.XSIZE;
- final int ysize = parent.map.YSIZE;
-
- private int map_xpos = 0;
- private int map_ypos = 0;
- private int map_xbase = 0;
- private int map_ybase = 0;
-
- int screen_xpos = 0;
- int screen_ypos = 0;
-
- private int save_x0 = 0; // used to compute translations when
- private int save_y0 = 0; // repositioning the map
- private int save_x1 = 0;
- private int save_y1 = 0;
-
- private int delta_x = 0;
- private int delta_y = 0;
-
- MapCanvas(MapInfo target) {
- super();
- this.parent = target;
- this.resize(xsize, ysize);
- }
-
- // Utility methods
- private final int scale(int param) {
- int mapscale = parent.mapview.scale_ctrls.getScale();
- return(java.lang.Math.round(param * mapscale / 100));
- }
-
- private final int unscale(int param) {
- int mapscale = parent.mapview.scale_ctrls.getScale();
- try {
- return(java.lang.Math.round(param * 100 / mapscale));
- }
- catch (java.lang.ArithmeticException e) {
- error_handler.display("Arithmetic exception:"+e.getMessage());
- return(0);
- };
- }
- private final Region scaleRegion(Region reg) {
- Polygon poly = reg.perim;
- Polygon scaledPoly =
- new Polygon(poly.xpoints, poly.ypoints, poly.npoints);
- Region scaledRegion = new Region();
- for (int i = 0; i < poly.npoints; i++) {
- scaledPoly.xpoints[i] = scale(poly.xpoints[i]);
- scaledPoly.ypoints[i] = scale(poly.ypoints[i]);
- }
- scaledRegion.perim = scaledPoly;
- return(scaledRegion);
- }
-
- // Translate the graphics back to top left corner
- public void topAlign() {
- synchronized (hiddengraphics) {
- hiddengraphics.translate(map_xbase, map_ybase);
- }
- map_xbase = 0;
- map_ybase = 0;
- }
-
- // Mouse methods
- public boolean mouseDown(Event evt, int x, int y){
- int Function = parent.ctrlview.getPanelFunction();
- if (Function == parent.ctrlview.getLocationInfo) {
- parent.ctrlview.display_layout.show((Container)parent.ctrlview.display,
- "info_display");
- parent.ctrlview.info_display.showLocationInfo(map_xpos, map_ypos);
- setSelectedLocation((Location)loc); // record location
- return true;
- } else if (Function == parent.ctrlview.getRouteInfo) {
- parent.ctrlview.display_layout.show((Container)parent.ctrlview.display,
- "route_display");
- //parent.ctrlview.info_display.showRouteInfo(map_xpos, map_ypos);
- setSelectedLocation((Location)loc); // record location
- return true;
- } else if (Function == parent.ctrlview.getDetailInfo) {
- parent.ctrlview.display_layout.show((Container)parent.ctrlview.display,
- "detail_display");
- // The following line may be used to generate the polygons for regions
- // defining locations, in Map.java.
- System.out.println("\tp.addPoint(" + map_xpos + ", " + map_ypos + ");");
- return true;
- } else { // default
- showLocationCoords(map_xpos, map_ypos);
- return true;
- }
- }
-
- public boolean mouseMove(Event evt, int x, int y){
- if (x < 0)
- {screen_xpos = 0;}
- else if (x > xsize)
- {screen_xpos = xsize;}
- else
- {screen_xpos = x;}
- if (y < 0)
- {screen_ypos = 0;}
- else if (y > ysize)
- {screen_ypos = ysize;}
- else
- {screen_ypos = y;}
- map_xpos = unscale(screen_xpos + map_xbase);
- map_ypos = unscale(screen_ypos + map_ybase);
- parent.setInfo("x:"+map_xpos+", y:"+map_ypos);
- loc = parent.map.getLocation(map_xpos, map_ypos);
- repaint();
- return true;
- }
-
-
- public boolean mouseDrag(Event evt, int x, int y){
- if ((x > 0) && (x < xsize) && (y > 0) && (y < ysize)) {
- save_x0 = save_x1;
- save_x1 = x;
- save_y0 = save_y1;
- save_y1 = y;
- delta_x = save_x1 - save_x0;
- delta_y = save_y1 - save_y0;
- // prevent that initial "jump" by making sure deltas are small
- if ((java.lang.Math.abs(delta_x) < 30) &&
- (java.lang.Math.abs(delta_y) < 30)) {
- if ((map_xbase - delta_x >=0) &&
- (map_ybase - delta_y >=0) &&
- (map_xbase - delta_x <= scale(xsize)-xsize) &&
- (map_ybase - delta_y <= scale(ysize)-ysize))
-
- {
- synchronized (hiddengraphics) {
- hiddengraphics.translate(delta_x, delta_y);
- }
- map_xbase = map_xbase - delta_x;
- map_ybase = map_ybase - delta_y;
- repaint();
- }
- }
- if (x < 0)
- {screen_xpos = 0;}
- else if (x > xsize)
- {screen_xpos = xsize;}
- else
- {screen_xpos = x;}
- if (y < 0)
- {screen_ypos = 0;}
- else if (y > ysize)
- {screen_ypos = ysize;}
- else
- {screen_ypos = y;}
- map_xpos = unscale(screen_xpos + map_xbase);
- map_ypos = unscale(screen_ypos + map_ybase);
- loc = parent.map.getLocation(map_xpos, map_ypos);
- return true;
- }
- return false;
- }
-
- /*-----------------
- GIS Methods
- -----------------*/
-
- // Show x-y coordinates of Location pointed to.
- public void showLocationCoords(int x, int y) {
- parent.ctrlview.showCoords(x, y);
- }
-
-
- // Return current Location
- public Location getLocation() {
- return(loc);
- }
-
- // Return current selected location
- public flashingLocation getSelectedLocation() {
- return(selected_location);
- }
-
- // Set the current selected location
- public void setSelectedLocation(Location loc) {
- if (loc != null) {
- selected_location = new flashingLocation(parent.map, loc, 500);
- selected_location.start();
- } else
- selected_location = null;
- }
-
- // Graphics methods
-
- public void paint(Graphics g) {
- if (hiddengraphics == null) {
- hiddenimagebuffer =
- createImage(this.size().width, this.size().height);
- hiddengraphics = hiddenimagebuffer.getGraphics();
- }
- int width = xsize;
- int height = ysize;
- // since we translate hiddengraphics in mouseDrag, we must
- // translate "back" to get true screen position for the cursor
- // crosshairs
- int true_x = screen_xpos + map_xbase;
- int true_y = screen_ypos + map_ybase;
-
- g.clipRect(0, 0, width+1, height+1);
- setBackground(Color.lightGray);
- synchronized (hiddengraphics) {
- hiddengraphics.clearRect(0,0,width,height);
- hiddengraphics.drawImage(parent.map.mapimage,
- 0, 0, scale(width), scale(height), this);
- }
- if (getSelectedLocation() != null) {
- flashingLocation loc = getSelectedLocation();
- if (loc.is_on()) {
- highlightLocation(loc, Color.blue);
- } else {
- highlightLocation(loc, Color.red);
- }
- }
- int Function = parent.ctrlview.getPanelFunction();
- if (Function == parent.ctrlview.getLocationInfo) {
- highlightLocation(loc);
- } else if (Function == parent.ctrlview.getRouteInfo) {
- highlightCarrier(loc);
- }
- synchronized (hiddengraphics) {
- if (loc != null) {
- hiddengraphics.setColor(Color.white);
- hiddengraphics.setFont(trb18);
- hiddengraphics.drawString(loc.getName(), true_x+5, true_y-5);
- hiddengraphics.drawString(loc.getName(), true_x+3, true_y-4);
- hiddengraphics.drawString(loc.getName(), true_x+4, true_y-3);
- hiddengraphics.setColor(Color.black);
- hiddengraphics.drawString(loc.getName(), true_x+4, true_y-4);
- }
- hiddengraphics.setColor(Color.blue);
- // vertical crosshair
- hiddengraphics.drawLine(true_x, 0, true_x, scale(ysize));
- // horizontal crosshair
- hiddengraphics.drawLine(0, true_y, scale(xsize), true_y);
- // frame around image
- hiddengraphics.setColor(Color.black);
- hiddengraphics.drawRect(map_xbase, map_ybase, width, height);
- // now refresh the actual screen
- }
- g.drawImage(hiddenimagebuffer, 0, 0, this);
- }
-
- public void repaint(Graphics g){
- paint(g);
- }
-
- public void update(Graphics g){
- paint(g);
- }
-
- // highlight the location at point loc in the hidden graphics
- public void highlightLocation(Location loc) {
- Color col;
- if (loc != null) {
- if (loc instanceof Road)
- col = Color.black;
- else if (loc instanceof Building)
- col = Color.red;
- else if (loc instanceof Park)
- col = Color.green;
- else
- col = Color.gray;
- if (loc.isDisplayable()) {
- synchronized (hiddengraphics) {
- hiddengraphics.setColor(col);
- hiddengraphics.fillPolygon(
- scaleRegion(loc.getRegion()).perim);
- }
- }
- }
- }
-
- // highlight the location at point loc in the hidden graphics
- // with a particular color
- public void highlightLocation(Location loc, Color col) {
- if (loc != null) {
- if (loc.isDisplayable()) {
- synchronized (hiddengraphics) {
- hiddengraphics.setColor(col);
- hiddengraphics.fillPolygon(
- scaleRegion(loc.getRegion()).perim);
- }
- }
- }
- }
-
- // highlight the carriers incident at location loc
- public void highlightCarrier(Location loc) {
- if (loc != null) {
- synchronized (hiddengraphics) {
- hiddengraphics.setColor(Color.blue);
- }
- for (Enumeration e = loc.exits.elements(); e.hasMoreElements();) {
- Carrier c = (Carrier)e.nextElement();
- Region r = c.getRegion();
- synchronized (hiddengraphics) {
- hiddengraphics.fillPolygon(scaleRegion(r).perim);
- }
- }
- for (Enumeration e = loc.entrances.elements(); e.hasMoreElements();) {
- Carrier c = (Carrier)e.nextElement();
- Region r = c.getRegion();
- synchronized (hiddengraphics) {
- hiddengraphics.fillPolygon(scaleRegion(r).perim);
- }
- }
- }
- }
- }
-
- class ScaleCtrls extends Panel {
- private MapInfo parent;
- private int scalefactor = 100; // scale factor in percent
- private int scale_incr = 20; // increase factor by this amnt
- private Label scale_label = new Label("Map Scale: 100%");
- private Button zoom = new Button("Zoom");
- private Button unzoom = new Button("Unzoom");
-
-
- ScaleCtrls(MapInfo target) {
- super();
- this.parent = target;
- setLayout(new GridLayout(1, 3));
- add(scale_label);
- add(zoom);
- add(unzoom);
- }
-
- public int getScale() {
- return scalefactor;
- }
-
- public boolean handleEvent(Event evt) {
- if (evt.target instanceof Button) {
- Button b = (Button)evt.target;
- if (b == zoom) {
- if (scalefactor + scale_incr <= parent.map.MaxScale) {
- scalefactor += scale_incr;
- }
- }
- if (b == unzoom) {
- if (scalefactor - scale_incr >= parent.map.MinScale) {
- scalefactor -= scale_incr;
- parent.mapview.canvas.topAlign();
- }
- }
- scale_label.setText("Map Scale: "+scalefactor+"%");
- parent.mapview.canvas.repaint();
- return true;
- }
- else {
- return false;
- }
- }
- }
-